<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Cache;
use think\cache\driver\Redis;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    /**
     * 获取banner
     *
     */
    public function banner()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);

        $banner = Cache::get('banner');

        if (!$banner) {//缓存中无数据
            $data = Db::table('fa_banner')
                ->field(['createtime', 'updatetime'], true)
                ->where('status', '1')
                ->order('weigh desc')
                ->select();

            if ($data) {//当查询到数据时存入缓存

                Cache::set('banner', $data, 0);
            }
            $this->success('ok', ['bannerList' => $data], 200);

        }

        $this->success('ok', ['bannerList' => $banner], 200);

    }

    /**
     * 获取员工
     * @param poi_id //根据poi获取员工
     */
    public function getWorkerList()
    {
        if ($this->request->isPost()) {

            $poi_ids = trim($this->request->post('poi_ids'));

            if (!$poi_ids) {

                $this->success('缺少必要参数！', null, 201);

            }

            $poi_ids = explode(",", $poi_ids);

            $village = [];

            foreach ($poi_ids as $index => $poi_id) {

                $village = Db::table('fa_village')->where('poi_id', $poi_id)->find();

                if ($village || $index >= 30) {
                    break;
                }

            }

            if (!$village) {//没有获取到小区
                $this->success('没有获取到小区！', ['workerList' => []], 200);
            }

            $workerList = Db::table('fa_worker')
                ->field(['id', 'age', 'avatar', 'gender', 'name', 'score', 'service_count'])
                ->where('group_id', $village['worker_group_id'])
                ->where('deletetime', null)
                ->select();

            $this->success('ok', ['workerList' => $workerList], 200);
        }

    }

    /**
     * 获取服务列表
     *
     */
    public function service()
    {
        $hotServiceList = Cache::get('hotServiceList');

        $generalServiceList = Cache::get('generalServiceList');

        if (!$hotServiceList || !$generalServiceList) {
            $hotServiceList = Db::table('fa_service')
                ->field(['createtime', 'updatetime', 'deletetime'], true)
                ->where('status', 1)
                ->where('deletetime', null)
                ->order('weigh desc')
                ->select();
            $generalServiceList = Db::table('fa_service')
                ->field(['createtime', 'updatetime', 'deletetime'], true)
                ->where('status', 0)
                ->where('deletetime', null)
                ->order('weigh desc')
                ->select();
            if ($hotServiceList) {

                Cache::set('hotServiceList', $hotServiceList, 0);

            }
            if ($generalServiceList) {

                Cache::set('hotServiceList', $hotServiceList, 0);

            }

        }


        $this->success('ok', ['hotList' => $hotServiceList, 'generalList' => $generalServiceList], 200);
    }
}




































